<?php
/**
 * Created by PhpStorm.
 * User: wangye
 */
try{
    $res = 1+1;
    if ($res=2){
        throw new Exception("这娃作弊,折磨难的题,都对了",110);
    }
}catch (Exception $e){
    echo $e->getMessage();
    echo $e->getCode();
}

#################################################
class Excapationphp extends Exception{
    public function fileInfo()
    {
        return 'file info';
    }
}

try{
    //open file
    throw new Excapationphp('异常',001);
}catch (Excapationphp $e){
    return $e->fileInfo();
}

###################################################
class FileEmpException extends Exception{}

####

class FilePermissionException extends Exception{}

try{
    throw new FileEmpException('s');
    throw new FilePermissionException('ms');
}catch (FileEmpException $e){
    echo $e->getMessage();
}catch (FilePermissionException $e){
    echo $e->getMessage();
}
###################################################
class FileException extends Exception{
    const FILE_NOT_EXITS = 1;#文件不存在
    const FILE_PERMISSION = 2;#权限不够
}

function fileHandle(){
    #文件不存在
    throw new FileException('FilenotExist',FileException::FILE_NOT_EXITS);
    #文件权限不够
    throw new FileException('FilenotExist',FileException::FILE_NOT_EXITS);
}

try{
    fileHandle();
}catch (FileException $e){
    if ($e->getCode()==1){

    }
    if ($e->getCode()==2){

    }
}catch (Exception $e){

}
#####################全局异常处理##############################
function defaultExceptionHandle($e){
    echo "Uncaught exception:{$e->getMessage()},file{$e->getFile()}";
}

set_exception_handler('defaultExceptionHandle');

throw new Exception('test');